package com.liang.exception;

import java.util.Scanner;

public class ExceptionDemo3 {
    public static void main(String[] args) {
        //创建学生对象，并且键盘录入学生的信息
        Student stu = new Student();
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入姓名");
        String name = sc.nextLine();
        stu.setName(name);

        //输入学生年龄
        //要求：学生年龄在16-20之间
        //如果录入信息有误，重新录入，直到成功为止
        while (true) {
            System.out.println("请输入学生年龄");
            String s = sc.nextLine();

            try {
                int age = Integer.parseInt(s);
                stu.setAge(age);
                break;
            } catch (NumberFormatException e) {
                System.out.println("年龄格式不正确");
            } catch (AgeOutOfBoundException e) {
//                System.out.println(e.toString());
                e.printStackTrace();
            }

        }
        System.out.println(stu.toString());

    }

}
